package com.xjscrm.server.config;

import com.uzai.trace.TraceIdThreadPoolExecutor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.CustomizableThreadFactory;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author jianghan
 * @date 2017-11-09 17:01
 */
@Configuration
@EnableAsync
public class TaskExecutorConfig {
    private int corePoolSize_event = 200;
    private int maxPoolSize_event = 500;
    private int queueCapacity_event = 200;
    private int keepAlive_event = 60;

    @Bean(name="taskExecutorForEvent")
    public TaskExecutor taskExecutorForEvent() {
//        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//        executor.setCorePoolSize(corePoolSize);
//        executor.setMaxPoolSize(maxPoolSize);
//        executor.setQueueCapacity(queueCapacity);
//        executor.setThreadNamePrefix("scrm-server-task-");
//        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
//        executor.setKeepAliveSeconds(keepAlive);
//        executor.initialize();
//        return executortor;

        TraceIdThreadPoolExecutor executor = new TraceIdThreadPoolExecutor(corePoolSize_event, maxPoolSize_event, keepAlive_event, TimeUnit.MINUTES, new LinkedBlockingQueue<>(queueCapacity_event));
        executor.setThreadFactory(new CustomizableThreadFactory("scrm-server-task-event"));
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        return executor;
    }

    private int corePoolSize_job = 100;
    private int maxPoolSize_job = 200;
    private int queueCapacity_job = 200;
    private int keepAlive_job = 60;

    @Bean(name="taskExecutorForXxJob")
    public TaskExecutor taskExecutorForXxJob() {
//        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//        executor.setCorePoolSize(corePoolSize);
//        executor.setMaxPoolSize(maxPoolSize);
//        executor.setQueueCapacity(queueCapacity);
//        executor.setThreadNamePrefix("scrm-server-task-");
//        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
//        executor.setKeepAliveSeconds(keepAlive);
//        executor.initialize();
//        return executortor;

        TraceIdThreadPoolExecutor executor = new TraceIdThreadPoolExecutor(corePoolSize_job, maxPoolSize_job, keepAlive_job, TimeUnit.MINUTES, new LinkedBlockingQueue<>(queueCapacity_job));
        executor.setThreadFactory(new CustomizableThreadFactory("scrm-server-task-xxljob"));
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        return executor;
    }


    private int corePoolSize_msg = 20;
    private int maxPoolSize_msg = 50;
    private int queueCapacity_msg = 200;
    private int keepAlive_msg = 60;

    @Bean(name="taskExecutorForMsg")
    public TaskExecutor taskExecutorForMsg() {
//        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//        executor.setCorePoolSize(corePoolSize);
//        executor.setMaxPoolSize(maxPoolSize);
//        executor.setQueueCapacity(queueCapacity);
//        executor.setThreadNamePrefix("scrm-server-task-");
//        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
//        executor.setKeepAliveSeconds(keepAlive);
//        executor.initialize();
//        return executortor;

        TraceIdThreadPoolExecutor executor = new TraceIdThreadPoolExecutor(corePoolSize_msg, maxPoolSize_msg, keepAlive_msg, TimeUnit.MINUTES, new LinkedBlockingQueue<>(queueCapacity_msg));
        executor.setThreadFactory(new CustomizableThreadFactory("scrm-server-task-msg"));
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        return executor;
    }

    private int corePoolSize_newCustomer = 50;
    private int maxPoolSize_newCustomer = 100;
    private int queueCapacity_newCustomer = 200;
    private int keepAlive_newCustomer = 60;
    @Bean(name="taskExecutorForNewCustomer")
    public TaskExecutor taskExecutorForNewCustomer() {
//        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//        executor.setCorePoolSize(corePoolSize);
//        executor.setMaxPoolSize(maxPoolSize);
//        executor.setQueueCapacity(queueCapacity);
//        executor.setThreadNamePrefix("scrm-server-task-");
//        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
//        executor.setKeepAliveSeconds(keepAlive);
//        executor.initialize();
//        return executortor;

        TraceIdThreadPoolExecutor executor = new TraceIdThreadPoolExecutor(corePoolSize_newCustomer, maxPoolSize_newCustomer, keepAlive_newCustomer, TimeUnit.MINUTES, new LinkedBlockingQueue<>(queueCapacity_newCustomer));
        executor.setThreadFactory(new CustomizableThreadFactory("scrm-server-task-newcustomer"));
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        return executor;
    }

    private int corePoolSize_friendReq = 50;
    private int maxPoolSize_friendReq = 100;
    private int queueCapacity_friendReq = 200;
    private int keepAlive_friendReq = 60;
    @Bean(name="taskExecutorForFriendReq")
    public TaskExecutor taskExecutorForFriendReq() {
//        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//        executor.setCorePoolSize(corePoolSize);
//        executor.setMaxPoolSize(maxPoolSize);
//        executor.setQueueCapacity(queueCapacity);
//        executor.setThreadNamePrefix("scrm-server-task-");
//        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
//        executor.setKeepAliveSeconds(keepAlive);
//        executor.initialize();
//        return executortor;

        TraceIdThreadPoolExecutor executor = new TraceIdThreadPoolExecutor(corePoolSize_friendReq, maxPoolSize_friendReq, keepAlive_friendReq, TimeUnit.MINUTES, new LinkedBlockingQueue<>(queueCapacity_friendReq));
        executor.setThreadFactory(new CustomizableThreadFactory("scrm-server-task-friendreq"));
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        return executor;
    }

    private int corePoolSize_qywxCallback = 50;
    private int maxPoolSize_qywxCallback = 100;
    private int queueCapacity_qywxCallback = 200;
    private int keepAlive_qywxCallback = 60;
    @Bean(name="taskExecutorForQywxCallback")
    public TaskExecutor taskExecutorForQywxCallback() {
//        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//        executor.setCorePoolSize(corePoolSize);
//        executor.setMaxPoolSize(maxPoolSize);
//        executor.setQueueCapacity(queueCapacity);
//        executor.setThreadNamePrefix("scrm-server-task-");
//        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
//        executor.setKeepAliveSeconds(keepAlive);
//        executor.initialize();
//        return executortor;

        TraceIdThreadPoolExecutor executor = new TraceIdThreadPoolExecutor(corePoolSize_qywxCallback, maxPoolSize_qywxCallback, keepAlive_qywxCallback, TimeUnit.MINUTES, new LinkedBlockingQueue<>(queueCapacity_qywxCallback));
        executor.setThreadFactory(new CustomizableThreadFactory("scrm-server-task-qywxcallback"));
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        return executor;
    }
}
